//
//  JXNewfeatureController.swift
//  01-weiboSwift
//
//  Created by TwistFate on 16/2/29.
//  Copyright © 2016年 TwistFate. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"

private let itemCount = 4

class JXNewfeatureController: UICollectionViewController {
    
    var layout : UICollectionViewFlowLayout = UICollectionViewFlowLayout()
    init () {
        super.init(collectionViewLayout: layout)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        layout.itemSize = (collectionView?.bounds.size)!
        layout.minimumLineSpacing = 0
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        collectionView?.bounces = false
        collectionView?.pagingEnabled = true
        collectionView?.showsHorizontalScrollIndicator = false
        
        self.collectionView!.registerClass(JXNewfeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)
    }

}

extension JXNewfeatureController {
    
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return itemCount
        
    }
    
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell  = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! JXNewfeatureCell
        
        cell.imageIndex = indexPath.item
        
//        cell.startAnimation()
        cell.enterButton.hidden = true
        
        return cell
    }
    
    override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        
        let count = Int(scrollView.contentOffset.x/scrollView.frame.width)
        if count == 3 {
            
            let indexPath = NSIndexPath(forItem: count , inSection: 0)
            let cell = collectionView?.cellForItemAtIndexPath(indexPath) as! JXNewfeatureCell
            cell.startAnimation()
        }
        
    }
}

class JXNewfeatureCell: UICollectionViewCell {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        prepareUI()
    }
    
    
    private func prepareUI () {
        contentView.addSubview(bkgImageView)
        contentView.addSubview(enterButton)
        
        bkgImageView.translatesAutoresizingMaskIntoConstraints = false
        enterButton.translatesAutoresizingMaskIntoConstraints = false
        
        contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[bkgView]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["bkgView":bkgImageView]))
        contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[bkgView]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["bkgView":bkgImageView]))
        
        contentView.addConstraint(NSLayoutConstraint(item: enterButton, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        contentView.addConstraint(NSLayoutConstraint(item: enterButton, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -160))
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func startAnimation () {
        enterButton.hidden = false
        enterButton.transform = CGAffineTransformMakeScale(0, 0)
        
        UIView.animateWithDuration(1, delay: 0.1, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            self.enterButton.transform = CGAffineTransformMakeScale(1, 1)
            }, completion: { (_) -> Void in
                print("开始体验 动画结束")
        })
    }
    
    var imageIndex: Int? = 0 {
        // 当collectionView 来设置imageIndex的时候,根据imageIndex去加载图片
        didSet {
            bkgImageView.image = UIImage(named: "new_feature_\(imageIndex! + 1)")
            enterButton.hidden = true
        }
    }
    
    lazy var bkgImageView : UIImageView = UIImageView ()
    
    lazy var enterButton : UIButton = {
        let button = UIButton()
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button_Highlighted"), forState: UIControlState.Highlighted)
        button.setTitle("开始体验", forState: UIControlState.Normal)
        button.addTarget(self, action: "startButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    }()
    
    func startButtonClick () {
        let appDelegate = UIApplication.sharedApplication().delegate as? AppDelegate
        appDelegate?.switchRootController(true)
    }
}

